﻿using NewLife;
using NewLife.Data;
using NewLife.Net;
using System.Text;

namespace TK.Socket
{
    public class AppSession : NetSession<AppServer>, ISend
    {
        /// <summary>
        /// Connected
        /// </summary>
        protected override void OnConnected()
        {
            var data = new DataPacket
            {
                SessionSend = this,
                DataType = DataType.Connect,
                SessionId = ID,
                Server = Session?.Local?.ToString(),
                Client = Session?.Remote?.ToString()
            };
            Host.CallbackData(data);
        }

        /// <summary>
        /// Disconnected
        /// </summary>
        protected override void OnDisconnected()
        {
            var data = new DataPacket
            {
                SessionSend = this,
                DataType = DataType.DisConnect,
                SessionId = ID,
                Server = Session?.Local?.ToString(),
                Client = Session?.Remote?.ToString()
            };
            Host.CallbackData(data);
        }

        /// <summary>
        /// Receive,有自定义管理处理程序，则不会走此事件
        /// </summary>
        protected override void OnReceive(ReceivedEventArgs e)
        {
            var framePacket = e.Message as Packet;
            var data = new DataPacket
            {
                SessionSend = this,
                DataType = DataType.Receive,
                SessionId = ID,
                Server = Session?.Local?.ToString(),
                Client = Session?.Remote?.ToString(),
                Data = framePacket.ToArray()
            };
            Host.CallbackData(data);
        }

        /// <summary>
        /// Error
        /// </summary>
        protected override void OnError(object sender, ExceptionEventArgs e)
        {
            var data = new DataPacket
            {
                SessionSend = this,
                DataType = DataType.Connect,
                SessionId = ID,
                Server = Session?.Local?.ToString(),
                Client = Session?.Remote?.ToString(),
                Message = $"{e.Action}|{e.Exception?.Message}->{e.Exception?.StackTrace}"
            };
            Host.CallbackData(data);
        }

        #region ISend
        /// <summary>
        /// 当前链接发送数据
        /// </summary>
        public void Send(byte[] data)
        {
            if (data != null && data.Length > 0)
            {
                base.Send(new Packet(data));
            }
        }

        /// <summary>
        /// 特定sessionId链接发送数据
        /// </summary>
        public void Send(int sessionId, byte[] data)
        {
            if (data != null && data.Length > 0)
            {
                var session = base.Host.GetSession(sessionId);
                if (session != null)
                {
                    session.Send(new Packet(data));
                }
            }
        }

        /// <summary>
        /// 关联服务的所有客户端发送数据
        /// </summary>
        public void SendAll(byte[] data)
        {
            if (data != null && data.Length > 0)
            {
                base.Host.SendAllAsync(new Packet(data));
            }
        }

        /// <summary>
        /// 关闭当前链接,一般服务端不主动关闭链接
        /// </summary>
        public void Close()
        {
            base.Dispose(true);
        }

        #endregion
    }
}
